package cn.jiyun.service.impl;

import cn.jiyun.model.pojo.User;
import cn.jiyun.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.jiyun.mapper.UserMapper;
import cn.jiyun.model.query.UserQuery;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

/**
 * 针对表【-User】的业务操作的Service实现类
 *
 * @author
 * @since 2024-12-18
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    private final UserMapper UserMapper;

    public UserServiceImpl(UserMapper UserMapper) {
        this.UserMapper = UserMapper;
    }

    @Override
    public IPage<User> getPageList(UserQuery query){
        //1.创建查询条件
        QueryWrapper<User> qw = new QueryWrapper<>();
        //ToDo 查询条件

        //2.组件分页数据
        IPage<User> page = new Page<>(query.getCurrentPage(), query.getPageSize());
        //3.查询
        baseMapper.selectPage(page,qw);
        //4.返回
        return page;
    }

    @Override
    public List<User> getCoupon(Long id ) {
        // 如果 status 为空或 null，则不使用 status 进行过滤
//        if (status == null || status.trim().isEmpty()) {
            return UserMapper.selectList(new QueryWrapper<User>().eq("id", id));
//        } else {
//            return UserMapper.selectList(new QueryWrapper<User>().eq("id", id).eq("usage_status", status));
//        }
    }

    @Override
    public int getCountCoupon(Long id) {
        return UserMapper.showCount(id);
    }

    @Override
    public Integer updateMonuey(Integer id,Integer money) {
        Integer currentBalance = money;
        if(currentBalance >= 1000 && currentBalance < 3000){
            currentBalance +=100;
        }else if(currentBalance >= 3000 && currentBalance < 5000){
            currentBalance +=400;
        }else if(currentBalance >= 5000){
            currentBalance +=800;
        }
//        System.out.println(currentBalance);
        UserMapper.updateMoney(id,currentBalance);
        return currentBalance;
    }

    @Override
    public void updatePrice(User user) {
        UserMapper.updatePrice(user.getId(), user.getAccountBalance());
    }
}
